#include "StdAfx.h"
#include ".\jhhandwriting.h"

CJhHandWriting::CJhHandWriting(void)
{
	Address = NULL;
	m_bInit = false;
	m_bRecogEnable = false;

//	hinstLib = NULL;
//	pInitFunc = NULL;
//	pExitFunc = NULL;
//	pRecogFunc = NULL;
}

CJhHandWriting::~CJhHandWriting(void)
{

	ExitHZRecog( );

	/*
	if( hinstLib )
	{
		FreeLibrary(hinstLib);
		hinstLib = NULL;
	}
	*/

}

bool CJhHandWriting::InitHzFunc(void)
{
	return true;
/*
	WCHAR szDllPath[260] = {0};
	wsprintf( szDllPath , L"%s\\HZRecog.dll", globle.GetWorkingDir() );
	hinstLib = LoadLibrary( szDllPath ); 
	if( hinstLib )
	{
		
		//pInitFunc = (_pHZInitCharacterRecognition) GetProcAddress(hinstLib, "HZInitCharacterRecognition"); 
		//pExitFunc = (_pHZExitCharacterRecognition) GetProcAddress(hinstLib, "HZExitCharacterRecognition");
        //pRecogFunc = (_pHZCharacterRecognize) GetProcAddress(hinstLib, "HZCharacterRecognize");
		
		pInitFunc = HZInitCharacterRecognition;
		pExitFunc = HZExitCharacterRecognition;
		pRecogFunc = HZCharacterRecognize;

		if( pInitFunc && pExitFunc && pRecogFunc )
			return true;	
	}
*/
	return false;
}

bool CJhHandWriting::InitHzRecog()
{
	int bFlag = 0; 

	void * pData = (void*)&szJHWRDIC[0];
	//bFlag = pInitFunc(pData);
	bFlag = HZInitCharacterRecognition( pData );

	if( bFlag )
	{
		HZSetParam(PARAM_RECORANG, RECOG_RANGE_ALL);
		return true;
	}
	else
	{
		return false;
	}
	return true;

	/*

	WCHAR szDllPath[260] = {0};
	wsprintf( szDllPath , L"%s\\SYSTEM\\JHWRDIC.DAT", globle.GetWorkingDir() );

	
	CFile  cf;
	if( cf.Open(  szDllPath, CFile::modeRead | CFile::shareDenyNone ) )
	{
		DWORD dwFileSize = (DWORD)cf.GetLength();
        Address = (BYTE *)malloc(dwFileSize);
		if(Address )
		{
			cf.Read( Address, dwFileSize );		
		}	

		cf.Close();

		if( Address )
		{
			bFlag = HZInitCharacterRecognition(Address);
			if( bFlag )
			{
				HZSetParam(PARAM_RECORANG, RECOG_RANGE_ALL);
				return true;
			}			
		}
	}
	*/

	/*
	long lpos;
	FILE *fp = fopen(".\\jHWRDic.dat","rb");
	if( fp )
	{
		fseek(fp,0,SEEK_END);
		lpos = ftell(fp);
		Address = (BYTE *)malloc(lpos);
		if(Address == NULL)
		{
			printf("malloc failed!");
			return false;
		}
		fseek(fp,0,SEEK_SET);
		fread(Address,1,lpos,fp);
		fclose(fp);
		{
			//int bFlag = HZInitCharacterRecognition(Address);
			int bFlag = pInitFunc(Address);
		}

		return true;
	}	
	*/

	if( bFlag )
		return true;
	else
	    return false;
}

void CJhHandWriting::ExitHZRecog()
{
	//HZExitCharacterRecognition();
//	if( pExitFunc )
//	    pExitFunc();

	HZExitCharacterRecognition( );
	
	if (Address != NULL)
		free(Address);
}


bool CJhHandWriting::OpenRecog(void)
{
	if( m_bInit == false )
	{
		m_bInit = true;
		if( InitHzFunc() && InitHzRecog() )
		{
			m_bRecogEnable = true;
		}
		else
		{
			m_bRecogEnable = false;
		}
	}

	return m_bRecogEnable;
}

bool CJhHandWriting::Recognize(short* pStroke, unsigned short* pResult)
{
	if(  m_bInit == false && OpenRecog() == false )
		return false;

	if( m_bRecogEnable == false )
		return false;

	int ret = 0;
	//ret = pRecogFunc(pStroke,pResult);	
	ret = HZCharacterRecognize( pStroke, pResult);
	if(ret != 10)
		return false;
	else
		return true;

	return false;
}
